//
//  VCOfClassify.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/5.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfClassify.h"
#import "XYMacro.h"

@interface VCOfClassify () <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *backCollectionView;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) UICollectionView *topBarCollection;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, assign) NSInteger labelIndex;
@property (nonatomic, strong) NSIndexPath *oldIndexPath;
@property (nonatomic, strong) ClassifyModelFirst *modelFirst;
@property (nonatomic, strong) ClassifyModelSecond *modelSecond;
@property (nonatomic, strong) ClassifyModelSecond *modelThird;
@property (nonatomic, strong) NSString *cat_id;
@end

@implementation VCOfClassify

- (void)viewDidLoad {
    [super viewDidLoad];
    _arr = [NSMutableArray arrayWithObject:@"精选"];
    [self handleData];
    self.oldIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self configNavigationController];
    [self configViewController];
}

- (void)handleData {
    
    NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22channel_id%22%3A%22";
    NSString *str2 = @"%22%7D";
    NSString *body = [[str1 stringByAppendingString:_channel_id] stringByAppendingString:str2];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/channelinfo.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _modelFirst = [ClassifyModelFirst modelWithDictionary:result];
        
        for (ClassifyModelFirstCatList *model in _modelFirst.data.cat_list) {
            [_arr addObject:model.cat_name];
        }
        [_topBarCollection reloadData];
        [_backCollectionView reloadData];
    } andFailureBlock:^(NSError *error) {}];

}


- (void)configNavigationController {
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    UIButton *goBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackButton.frame = CGRectMake(0, 0, 30, 30);
    [goBackButton setImage:[UIImage imageNamed:@"go_back"] forState:UIControlStateNormal];
    [goBackButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:goBackButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}


- (void)configViewController {
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _backView.backgroundColor = BACKGROUNDCOLOR;
    [self.view addSubview:_backView];
    [self createTopBar];
    [self createBigBackCollectionView];
    
    
    
}


- (void)createTopBar {
    UIView *lineOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    lineOne.backgroundColor = [UIColor colorWithRed:0.44 green:0.45 blue:0.47 alpha:1.00];
    [_backView addSubview:lineOne];
    
    UICollectionViewFlowLayout *layoutOne = [[UICollectionViewFlowLayout alloc] init];
    layoutOne.minimumLineSpacing = 0;
    layoutOne.minimumInteritemSpacing = 0;
    layoutOne.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layoutOne.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _topBarCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 1, SCREEN_WIDTH, 48) collectionViewLayout:layoutOne];
    _topBarCollection.delegate = self;
    _topBarCollection.dataSource = self;
    _topBarCollection.bounces = NO;
    _topBarCollection.showsHorizontalScrollIndicator = NO;
    [_backView addSubview:_topBarCollection];
    [_topBarCollection registerClass:[DiscoverClassifyTopPageBarCell class] forCellWithReuseIdentifier:@"DiscoverClassifyTopPageBarCell"];
    
    UIView *lineTwo = [[UIView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
    lineTwo.backgroundColor = [UIColor colorWithRed:0.44 green:0.45 blue:0.47 alpha:1.00];
    [_backView addSubview:lineTwo];

}

- (void)createBigBackCollectionView {
    
    UICollectionViewFlowLayout *layoutTwo = [[UICollectionViewFlowLayout alloc] init];
    layoutTwo.minimumLineSpacing = 0;
    layoutTwo.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layoutTwo.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _backCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 114) collectionViewLayout:layoutTwo];
    _backCollectionView.delegate = self;
    _backCollectionView.dataSource = self;
    _backCollectionView.bounces = NO;
    _backCollectionView.pagingEnabled = YES;
    _backCollectionView.scrollEnabled = NO;
    _backCollectionView.showsHorizontalScrollIndicator = NO;
    [_backView addSubview:_backCollectionView];
    [_backCollectionView registerClass:[DiscoverClassifyCollectionViewCellFirst class] forCellWithReuseIdentifier:@"DiscoverClassifyCollectionViewCellFirst"];
    [_backCollectionView registerClass:[DiscoverClassifyCollectionViewCellSecond class] forCellWithReuseIdentifier:@"DiscoverClassifyCollectionViewCellSecond"];
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_modelFirst.status.succeed == 1 ) {
        return _arr.count;
    }else {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _backCollectionView) {
        if (indexPath.row == 0) {
            DiscoverClassifyCollectionViewCellFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverClassifyCollectionViewCellFirst" forIndexPath:indexPath];
            cell.model = _modelFirst.data;
            return cell;
        } else {
            DiscoverClassifyCollectionViewCellSecond *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverClassifyCollectionViewCellSecond" forIndexPath:indexPath];
            cell.modelOne = _modelSecond;
            cell.modelTwo = _modelThird;
            cell.cat_id = _cat_id;
            return cell;
        }
        
    } else {
        DiscoverClassifyTopPageBarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverClassifyTopPageBarCell" forIndexPath:indexPath];
        
        if (indexPath.row == _labelIndex) {
            cell.titleLabel.textColor = [UIColor whiteColor];
            cell.titleLabel.font = [UIFont systemFontOfSize:22];
        } else {
            
            cell.titleLabel.textColor = [UIColor colorWithRed:0.33 green:0.35 blue:0.36 alpha:1.00];
            cell.titleLabel.font = [UIFont systemFontOfSize:17];
        }
        
        cell.backgroundColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
        cell.titleLabel.text = [_arr objectAtIndex:indexPath.row];
        return cell;
        
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == _topBarCollection) {
        
        if (indexPath.item != 0) {
            
            ClassifyModelFirstCatList *model = [_modelFirst.data.cat_list objectAtIndex:(indexPath.item - 1)];
            _cat_id = [NSString stringWithFormat:@"%ld", model.cat_id];

            NSString *str1 = @"json=%7B%22cat_id%22%3A%22";
            NSString *str2 = @"%22%2C%22orderby%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22searchtype%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22target_id%22%3A%220%22%7D";
            NSString *body = [[str1 stringByAppendingString:_cat_id] stringByAppendingString:str2];
            [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
                _modelSecond = [ClassifyModelSecond modelWithDictionary:result];
                _modelSecond.tag = 1;
                [_backCollectionView reloadData];
            } andFailureBlock:^(NSError *error) {}];
            
            
            NSString *str3 = @"json=%7B%22cat_id%22%3A%22";
            NSString *str4 = @"%22%2C%22orderby%22%3A1%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22searchtype%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22target_id%22%3A%220%22%7D";
            NSString *body1 = [[str3 stringByAppendingString:_cat_id] stringByAppendingString:str4];
            [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body1 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
                _modelThird = [ClassifyModelSecond modelWithDictionary:result];
                _modelThird.tag = 2;
                [_backCollectionView reloadData];
            } andFailureBlock:^(NSError *error) {}];
        }
        
        [_backCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        DiscoverClassifyTopPageBarCell *cell = (DiscoverClassifyTopPageBarCell *)[_topBarCollection cellForItemAtIndexPath:indexPath];
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.titleLabel.font = [UIFont systemFontOfSize:22];
        
        DiscoverClassifyTopPageBarCell *pastCell = (DiscoverClassifyTopPageBarCell *)[_topBarCollection cellForItemAtIndexPath:_oldIndexPath];
        if (indexPath != _oldIndexPath) {
            pastCell.titleLabel.textColor = [UIColor colorWithRed:0.33 green:0.35 blue:0.36 alpha:1.00];
            pastCell.titleLabel.font = [UIFont systemFontOfSize:17];
        }
        _oldIndexPath = indexPath;
        self.labelIndex = _oldIndexPath.item;

    }
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == _backCollectionView) {
        CGSize newSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 114);
        return newSize;
    } else {
        CGRect newRect = [[_arr objectAtIndex:indexPath.row ] boundingRectWithSize:CGSizeMake(0, 49) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
        CGSize newSize = CGSizeMake(newRect.size.width + 25, 48);
        return newSize;
        
    }
    
}



- (void)goBack:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
}

- (void)viewDidAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
